/*
NAME: 
DESCRIPTION: ;
PARAMETER:
    [
		{
			name: 'step_filter',
			title: 'step过滤',
			type: 'LineEdit'
		},
		{
			name : 'auto_save',
			title : '自动保存',
			type : 'RadioBox',
            property : {
				item_list:[
					{name:'yes',text:'Yes'},
					{name:'no',text:'No'},
				],
				tool_tip:'是否自动保存料号开关'
			}
        }
	]
	
 VERSION_HISTORY:
	V1.00 2020-04-20 Scott Sun
	    1.新版本
		
 HELP:
 	<html><body bgcolor="#DDECFE">
		<font size="3" color="#003DB2"><p>功能简介</p></font>
		<p> pad对齐 </p>
		<br>
		<font size="3" color="#003DB2"><p>参数配置</p></font>
		<p> </p>
		<br>
		<font size="3" color="#003DB2"><p>注意事项</p></font>
		<p> 无 </p>
		<br>
	</body></html>	
*/
//////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////
console.log("================================>加减物件");
// 引入模块 包
var $ = require('topcam.scriptfunc').argv();
var fs = require('fs');
var _ = require('lodash');
var mode = $.ikm ? "topcam" : "aimdfm";
var IKM = $.ikm; 
if (IKM==undefined ) { IKM = require('topcam.ikm6')($) }
var GEN = $.gen;
var GUI = $.gui || {};
var Job = $.job || $.job_name;
var JobId = $.job_id;
var db = $.db || IKM.db
var PAR = {}
if ($.par) {
	PAR = $.par
} else if ($.hasOwnProperty('script_parameter')){
	PAR = JSON.parse($.script_parameter);
}
if (mode === "aimdfm") {
	var database = require("topsin.database");
	database.addConnection($.conf.database_conf, "DFM");
	var QDfm = database.query("DFM");
	$.QDfm = QDfm;
	if ($.conf.product_type == "aimdfm") {
		QDfm.updateRow({
			table: "pdm_aimdfm_task",
			data: {
				current_process_title: $.process_title
			},
			where: { id: $.task_id }
		});
	}
}
var Status = 'ok';
var resultData = [];

var jtg_cam_tips = IKM.get_jobinfo({jobid:JobId, jobinfo:"jtg_cam_tips"})

try {
	var par = PAR;
	var default_par = {
        step_filter:'net',
        auto_save: "No",
        units: "mm"
	}
	for(var key in default_par){  // 设置默认属性
		if (!par.hasOwnProperty(key) || par[key] == ""){
			par[key] = default_par[key]
		}
	}

	if(_.isEmpty(Job)){throw "参数Job不存在"}
	var job = Job.toLowerCase()
	if(!GEN.isJobExists({job:job})){throw "料号"+job+"不存在"}
	if(!GEN.isJobOpen({job:job})){ GEN.openJob({job:job}) }
    if(mode == "aimdfm"){
        if(GEN.checkInout({job:job,mode:"test"}) != 0){ throw "the job check" }
        GEN.checkInout({job:job,mode:"out"});
    }

		
	
	GEN.workLayer({name:'阿苏大',display_number:2,clear_before:'yes'})


	//保存 
	if(/yes/ig.test(par.auto_save.toLowerCase())){  //
		GEN.checkInout({job:job,mode:"out"})  // 结束保存料号 关闭料号
		GEN.saveJob({ job: job });
		GEN.checkInout({job:job,mode:"in"})
		GEN.closeJob({job:job})
	} else {
		GEN.checkInout({job:job,mode:"in"})
	}

    if (mode === "aimdfm") {
		$.QDfm.updateRow({
			table: "pdm_aimdfm_task",
			data: {
				progress: 33.33
			},
			where: { id: $.task_id }
		});
		if (GEN.hasError()) {
			Status = 'error';
			resultData.push({ type: "error", title: "GEN错误!", detail: [{ desc: _.join(GEN.STATUS, "\n") }] });
			return {
				status: Status,
				result_data: resultData
			};
		} else {
			resultData.push({ type: "info", title: "操作完成, 请注意检查!" });
			return {
				status: Status,
				result_data: resultData
			};
		}
	}else {
		return "Done"
	}   
} catch (e) {
	// GEN.STATUS = [e]
	IKM.msg(jtg_cam_tips || "无");
	var func = "function(prop){return GUI.showForm(prop) }"
    var ret = IKM.command(func, {
		title: "确认是否加减物件",
		size: "200x120",
		items: {
			name: 'main',
			type: 'FormGridLayout',
			property: { margin: 18, spacing: 20, columns: 2 },
			pack: {},
			child: [
				{
					name: "chanegitem",
					type: "RadioBox",
					property: {
						item_list:[
							{name:"yes", text:"yes"},
							{name:"no", text:"no"}
						]
					},
					pack: {
						label: "是否加减物件",
						column_span: 2,
						label_alignment: "Left"
					},
				},

				{
					type: 'stretch'
				}
			]
		},
		values: {},
		self: this
	}, 1);
	if (ret.data.chanegitem == "yes") {
		IKM.msg("请加减物件")
	}
    Status = 'error';
    resultData.push({type: "error", title: "脚本执行出错!", detail: [{desc: _.toString(e)}]});
	return (mode === "aimdfm") ? {status: Status, result_data: resultData} : "Error";
}